﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;
using System.Windows.Forms;
using com.fukefu.sql;

namespace com.fukefu.report
{
    public partial class ReportForm : BaseForm
    {
        private static ReportForm rf;
        private static Object obj = new Object();

        public static ReportForm GetInstance()
        {
            if (rf == null || rf.IsDisposed)
            {
                lock (obj)
                {
                    if (rf == null || rf.IsDisposed)
                    {
                        rf = new ReportForm();
                    }
                }
            }
            return rf;
        }

        SqlServerOperate sql = new SqlServerOperate();
        List<string> list_database = new List<string>();
        List<string> list_sql = new List<string>();

        int sql_id = -1, database_id = 0;

        private ReportForm()
        {
            InitializeComponent();

            new Thread(new ThreadStart(Init)).Start();
        }
        private void Init()
        {
            SqlDataReader re;
            if (LoginUser.Name == null)
            {
                SetControl("combobox1", ControlValue.Combobox, "所有");
                /*加载所有城市*/
                re = sql.Query("select * from master..sysdatabases where name like '%智能群发%'");
                while (re.Read())
                {
                    list_database.Add(re["name"].ToString());
                    SetControl("combobox1", ControlValue.Combobox, re["name"]);
                }
                re.Close();
            }
            else
            {
                list_database.Add(Config.Sql_database);
                SetControl("combobox1", ControlValue.Combobox, Config.Sql_database);
            }
            //加载所有查询报表
            re = sql.Query("select * from [全局设置].[dbo].[_report_查询报表] order by weights desc");
            while (re.Read())
            {
                SetControl("combobox2", ControlValue.Combobox, re["title"]);
                list_sql.Add(re["sql"].ToString());
            }
            re.Close();
            //按钮
            SetControl("button1", ControlValue.Enable, true);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            new Thread(new ThreadStart(ShowListView)).Start();
        }
        //显示数据
        private void ShowListView()
        {
            //没有选择就结束
            if (sql_id > -1)
            {
                string d1 = GetControl("dt1", ControlValue.DateTimePicker).ToString();
                string d2 = GetControl("dt2", ControlValue.DateTimePicker).ToString();
                string cmd = list_sql[sql_id];
                cmd = cmd.Replace("{d1}", d1).Replace("{d2}", d2);
                List<ListViewItem> list2 = new List<ListViewItem>();
                int begin_id = database_id == 0 ? 0 : database_id - 1;
                int end_id = (database_id == 0 ? list_database.Count : database_id);
                for (int j = begin_id; j < end_id; j++)
                {
                    if (LoginUser.Uid == null && database_id == 0)
                    {
                        list2.Add(new ListViewItem(list_database[j].Replace("智能群发", "")));
                        list2.Add(new ListViewItem("-----------"));
                    }
                    SqlDataReader re = sql.Query("use [" + list_database[j] + "]" + "\r\n" + cmd);
                    int len = re.FieldCount;
                    if (j == begin_id)
                    {
                        ColumnHeader[] ch = new ColumnHeader[len];
                        for (int i = 0; i < len; i++)
                        {
                            ColumnHeader _ch = new ColumnHeader();
                            _ch.Text = re.GetName(i);
                            ch[i] = _ch;
                        }
                        SetControl("listview1", ControlValue.ListViewHeader, ch);
                    }
                    string[] str = new string[len];
                    while (re.Read())
                    {
                        string[] k = new string[len];
                        for (int i = 0; i < len; i++)
                        {
                            k[i] = re[i].ToString();
                        }
                        list2.Add(new ListViewItem(k));
                    }
                    re.Close();
                    list2.Add(new ListViewItem(""));
                }
                SetControl("listview1", ControlValue.ListView, list2);
                CreateListViewContentScriptMenu(listView1);
            }
            SetControl("button1", ControlValue.Enable, true);
        }
        //选择切换
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            sql_id = this.comboBox2.SelectedIndex;
            if (this.comboBox2.Text.IndexOf("今日") != -1)
            {
                dt1.Enabled = false;
                dt2.Enabled = false;
            }
            else
            {
                dt1.Enabled = true;
                dt2.Enabled = true;
            }
        }
        //城市切换
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            database_id = this.comboBox1.SelectedIndex;
        }
    }
}
